package com.attendance.www.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.TipsBean;
import com.attendance.www.hr.beans.UserEmployeeBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IApprovalService;
import com.attendance.www.hr.service.ITipsService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * tips Controller
 * @author zx
 * 2017-12-04
 */
@Controller
@RequestMapping("/tips")
public class TipsController {
	
	@Resource
	private ITipsService tipsService;
	@Resource
	private IApprovalService approvalService;
	
	/**
	 * 获取待审批个数信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getTips.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getTips(HttpServletRequest request) {
		UserEmployeeBean userEmployee = (UserEmployeeBean) request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if (userEmployee == null) {
			throw new ResultFaultException("1_00001");
		}
		TipsBean tipsBean=tipsService.getTips(userEmployee.getEmployeeId());
		return ResultMap.success(tipsBean);
	}
	
	/**
	 * 查看是否有审批权限
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getPrivilegeGroupIdType.do", method = { RequestMethod.POST})
	@ResponseBody
	public Object getPrivilegeGroupIdType(HttpServletRequest request) {
		UserEmployeeBean userEmployee = (UserEmployeeBean) request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if (userEmployee == null) {
			throw new ResultFaultException("1_00001");
		}
		if(userEmployee.getPrivilegeGroupId()==3 || userEmployee.getPrivilegeGroupId()==5){
			return ResultMap.success(true);
		}else{
			return ResultMap.success(false);
		}
	}
	
	/**
	 * 获取打卡时间 & 判断是否是休息日
	 * @param request
	 * @param tipsBean
	 * @return
	 */
	@RequestMapping(value = "/getAttendanceTime.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getAttendanceTime(HttpServletRequest request, @RequestBody TipsBean tipsBean) {
		UserEmployeeBean userEmployee = (UserEmployeeBean) request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if (userEmployee == null) {
			throw new ResultFaultException("1_00001");
		}
		if (tipsBean.getAttendanceDate()==null) {
			throw new ResultFaultException("0_00001");
		}
		TipsBean tipsBean2=new TipsBean();
		String[] timeString=approvalService.getActAttendance(tipsBean.getAttendanceDate(), userEmployee.getEmployeeId());
		if (timeString!=null) {
			tipsBean2.setStartTime(timeString[0]);
			tipsBean2.setEndTime(timeString[1]);
		} else {
			tipsBean2.setStartTime(null);
			tipsBean2.setEndTime(null);
		}
		//判断是否是休息日
		Integer isRestDay=tipsService.getAttendanceTime(tipsBean.getAttendanceDate(), userEmployee.getEmployeeId()).getIsRestDay();
		tipsBean2.setIsRestDay(isRestDay);
		return ResultMap.success(tipsBean2);
	}
}
